package com.bigzero.workflow.modules.process.controller;

import com.bigzero.workflow.common.exception.WorkflowException;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.common.model.TableDataInfo;
import com.bigzero.workflow.common.utils.PageUtil;
import com.bigzero.workflow.modules.process.entity.ListenerEntity;
import com.bigzero.workflow.modules.process.query.ListenerQuery;
import com.bigzero.workflow.modules.process.service.ListenerService;
import com.bigzero.workflow.modules.process.vo.ListenerVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 执行监听器
 *
 * @author fanxinxiong
 * @email 896934834@qq.com
 * @date 2025-02-14 22:18:27
 */
@RestController
@RequestMapping("/process/listener")
@Tag(name = "流程管理-执行监听器")
public class ListenerController{

    @Autowired
    private ListenerService listenerService;

    /**
     * 列表
     *
     * @param query 参数
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 1)
    @Operation(summary = "分页列表")
    @GetMapping("list")
    public TableDataInfo list(ListenerQuery query) {
        PageUtil.startPage();
        List<ListenerVo> list = listenerService.queryPage(query);
        return PageUtil.getDataTable(list);
    }

    /**
     * 详情
     *
     * @param userId 用户id
     * @return 用户详情
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 2)
    @Operation(summary = "详情")
    @Parameters({
        @Parameter(name = "userId", description = "用户id", schema = @Schema(type = "String"), required = true)
    })
    @GetMapping("info/{userId}")
    public R<ListenerEntity> info(@PathVariable String userId) {
        ListenerEntity model = listenerService.getById(userId);
        return R.ok(model);
    }

    /**
     * 保存
     *
     * @param entity 数据
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 3)
    @Operation(summary = "保存")
    @PostMapping("save")
    public R<String> save(@RequestBody ListenerEntity entity) {
        if(StringUtils.isBlank(entity.getListenerName())){
            throw new WorkflowException("监听器名称不能为空!");
        }
        if(StringUtils.isBlank(entity.getJavaClass())){
            throw new WorkflowException("java类不能为空!");
        }
        listenerService.insertOrUpdate(entity);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param id 主键
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 4)
    @Operation(summary = "删除")
    @Parameters({
       @Parameter(name = "id", description = "主键id", schema = @Schema(type = "String"), required = true)
    })
    @DeleteMapping("delete")
    public R<String> delete(@RequestBody String id){
        listenerService.deleteBylistenerId(id);
        return R.ok();
    }
}
